package com.ruoyi.web.controller.zhuangxiucontroller.shopController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.construction.Account;
import com.ruoyi.system.service.zhaungxiuservice.shopService.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 保证金Controller
 *
 * @author ruoyi
 * @date 2021-04-21
 */
@RestController
@RequestMapping("/system/account")
public class AccountController extends BaseController {

    @Autowired
    private IAccountService accountService;

    /**
     * 查询保证金列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Account account)
    {
        startPage();
        List<Account> list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    /**
     * 导出保证金列表
     */

    @Log(title = "保证金", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Account account)
    {
        List<Account> list = accountService.selectAccountList(account);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        return util.exportExcel(list, "account");
    }

    /**
     * 获取保证金详细信息
     */

    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Integer accountId)
    {
        return AjaxResult.success(accountService.selectAccountById(accountId));
    }

    /**
     * 新增保证金
     */

    @Log(title = "保证金", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addBZJ")
    public AjaxResult add(@RequestBody Account account)
    {
        String s = account.getSupplierIdNum().toString();//获取前端穿过来的 编号ID
        Account account1 = new Account();
        account1.setSupplierCode(s);
        List<Account> accounts = accountService.selectAccountList(account1);
        if(accounts.size() == 0){
            account.setSupplierCode(s);
            account.setModifiedTime(new Date());
            return toAjax(accountService.insertAccount(account));
        }else {
            account.setSupplierCode(s);
            return toAjax(accountService.updateAccount(account));
//            return AjaxResult.error("已设置保证金");
        }
    }

    /**
     * 修改保证金
     */

    @Log(title = "保证金", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Account account)
    {
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除保证金
     */

    @Log(title = "保证金", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Integer[] accountIds)
    {
        return toAjax(accountService.deleteAccountByIds(accountIds));
    }


}
